package phases

import (
	confv1beta1 "gopkg.openfuyao.cn/bkecommon/cluster/api/v1beta1"
	ctrl "sigs.k8s.io/controller-runtime"

	bkev1beta1 "gopkg.openfuyao.cn/cluster-api-provider-bke/api/v1beta1"
	"gopkg.openfuyao.cn/cluster-api-provider-bke/pkg/phaseframe"
	l "gopkg.openfuyao.cn/cluster-api-provider-bke/utils/log"
)

// TODO: new BKEPhase steps
// 1, replace XXX to your custom phase name
// 2, add phase cName to list.PhaseNameCNMap map
// 3. write NeedExecute function code
// 4. write Execute function code
// 5. register your phase in list.CommonPhases or list.DeployPhases or list.PostDeployPhases
//	  the phase is sequential execution

const (
	XXXName confv1beta1.BKEClusterPhase = "XXX"
)

type XXX struct {
	phaseframe.BasePhase
}

func NewXXX(ctx *phaseframe.PhaseContext) phaseframe.Phase {
	ctx.Log.NormalLogger = l.Named(XXXName.String())
	base := phaseframe.NewBasePhase(ctx, XXXName)
	return &XXX{base}
}

// ExecutePreHook is not implemented
//func (e *XXX) ExecutePreHook() error {
//  // TODO: your logic here, sure DefaultPreHook will be called in the end
//  ...
//	return e.BasePhase.DefaultPreHook()
//}

// ExecutePostHook is not implemented
//func (e *XXX) ExecutePostHook(err error) error {
//  // TODO: your logic here, sure DefaultPostHook will be called in the end
//  ...
//	return e.BasePhase.DefaultPostHook(err)
//}

func (e *XXX) Execute() (_ ctrl.Result, err error) {
	if err = e.reconcileXXX(); err != nil {
		return ctrl.Result{}, err
	}
	return ctrl.Result{}, nil
}

func (e *XXX) NeedExecute(old *bkev1beta1.BKECluster, new *bkev1beta1.BKECluster) (needExecute bool) {
	if !e.BasePhase.DefaultNeedExecute(old, new) {
		return false
	}

	// TODO: your logic here, sure DefaultNeedExecute will be called at first
	// ...
	return false
}

func (e *XXX) reconcileXXX() error {
	// TODO: add reconcile logic here, and return error if any error occurs
	// the ctx contains phase execution context, you can use it to get client, bkeCluster, scheme, log, etc.
	//ctx, c, bkeCluster, scheme, log := e.Ctx.Untie()
	panic("implement me")
}
